将代码附加到" jsoninput.json"使用FileStream和StreamWriter在Server.MapPath中生成文件

时间:2017-03-31 01:50:09

标签: c# filestream streamwriter

" jsonr"已从原始格式jsoninput.json附加。

jsoninput.json

  

[count:[{first:1,second:1,third:1},{first:2,second:2,third:2}]]

我希望能够保存对" jsoninput.json"所做的更改。将代码附加到jsonr jsoninput.json Server.MapPath FileStream,使用StreamWriterjsonr="[count:[{first:1,second:2,third:3},{first:11,second:22,third:33},{first:111,second:222,third:333}]]" using (FileStream fsAppend = new FileStream(Server.MapPath("~/jsoninput.json"), FileMode.Append, FileAccess.Write)) { StreamWriter sw = new StreamWriter(fsAppend); sw.Write(jsonr); sw.Flush(); sw.Close(); StreamWriter Stream = File.AppendText(Server.MapPath("~/jsoninput.json"),jsonr); Stream.Close(); fsAppend.Close(); } ,如下所示:

any

1 个答案:

答案 0 :(得分:1)

在添加新的json数组之前,

{ "data": [ { "name": "Person 1", "email": "test@test.com", "address": { "address1": "my address 1", "address2": "my address 2" } }, { "name": "Person 2", "email": "example@example.com", "address": { "address1": "my address 1", "address2": "my address 2" } } ] } 文件竞争如下:

string jsonr="{
                count:[
                        {first:1,
                         second:2,
                         third:3
                        },      
                        {first:11,
                         second:22,
                         third:33
                        },
                        {first:111,
                         second:222,
                         third:333
                        }
                      ]
              }";

目标json数组添加:

C#

jsonr代码将JsonString.json添加到string jsonPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "JsonString.json"); string jsonString = File.ReadAllText(jsonPath); var jsonrObject = JObject.Parse(jsonr)["count"]; JObject o = JObject.Parse(jsonString); string resultJson = o.Property("data").AddAfterSelf(new JProperty("count", jsonrObject)).ToString();

private static final int CAMERA_REQUEST_FIRST_IMAGE = 100;
    private static final int CAMERA_REQUEST_SECOND_IMAGE = 101;
    private static final int CAMERA_REQUEST_THIRD_IMAGE = 102;
    private static final int CAMERA_REQUEST_FOURTH_IMAGE = 103;
    private static final int CAMERA_REQUEST_FIFTH_IMAGE = 104;
    private static final int PICK_PICTURE_FIRST_IMAGE = 105;
    private static final int PICK_PICTURE_SECOND_IMAGE = 106;
    private static final int PICK_PICTURE_THIRD_IMAGE = 107;
    private static final int PICK_PICTURE_FOURTH_IMAGE = 108;
    private static final int PICK_PICTURE_FIFTH_IMAGE = 109;
    Boolean imageclick_1 = false, imageclick_2 = false, imageclick_3 = false, imageclick_4 = false, imageclick_5 = false;
    TextView textview_imagename1, textview_imagename2, textview_imagename3, textview_imagename4, textview_imagename5;
ImageView imageView1, imageView2, imageView3, imageView4, imageView5;
imageView1 = (ImageView) findViewById(R.id.imageview1);
        imageView1.setOnClickListener(this);
 imageView2 = (ImageView) findViewById(R.id.imageview2);
        imageView2.setOnClickListener(this);
 imageView3 = (ImageView) findViewById(R.id.imageview3);
        imageView3.setOnClickListener(this);
 imageView4 = (ImageView) findViewById(R.id.imageview4);
        imageView4.setOnClickListener(this);
 imageView5 = (ImageView) findViewById(R.id.imageview5);
        imageView5.setOnClickListener(this);

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case PICK_PICTURE_FIRST_IMAGE:
                if (resultCode == Activity.RESULT_OK) {
                    Uri selectedImage1 = data.getData();
                    String s1 = getRealPathFromURI(selectedImage1);
                    try {
                        images1 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage1);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    imageView1.setImageURI(selectedImage1);
                    String name1 = s1.substring(s1.lastIndexOf("/") + 1);
                    textview_imagename1.setText(name1);
                    imageView2.setVisibility(View.VISIBLE);
                    imageclick_1 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                }
                break;
            case PICK_PICTURE_SECOND_IMAGE:
                if (resultCode == Activity.RESULT_OK) {
                    Uri selectedImage2 = data.getData();
                    String s2 = getRealPathFromURI(selectedImage2);
                    try {
                        images2 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage2);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    imageView2.setImageURI(selectedImage2);
                    String name2 = s2.substring(s2.lastIndexOf("/") + 1);
                    textview_imagename2.setText(name2);
                    imageView3.setVisibility(View.VISIBLE);
                    imageclick_2 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                }
                break;
            case PICK_PICTURE_THIRD_IMAGE:
                if (resultCode == Activity.RESULT_OK) {
                    Uri selectedImage3 = data.getData();
                    String s3 = getRealPathFromURI(selectedImage3);
                    try {
                        images3 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage3);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    imageView3.setImageURI(selectedImage3);
                    String name3 = s3.substring(s3.lastIndexOf("/") + 1);
                    textview_imagename3.setText(name3);
                    imageView4.setVisibility(View.VISIBLE);
                    imageclick_3 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                }
                break;
            case PICK_PICTURE_FOURTH_IMAGE:
                if (resultCode == Activity.RESULT_OK) {
                    Uri selectedImage4 = data.getData();
                    String s4 = getRealPathFromURI(selectedImage4);
                    try {
                        images4 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage4);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    imageView4.setImageURI(selectedImage4);
                    String name4 = s4.substring(s4.lastIndexOf("/") + 1);
                    textview_imagename4.setText(name4);
                    imageView5.setVisibility(View.VISIBLE);
                    imageclick_4 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                }
                break;
            case PICK_PICTURE_FIFTH_IMAGE:
                if (resultCode == Activity.RESULT_OK) {
                    Uri selectedImage5 = data.getData();
                    String s5 = getRealPathFromURI(selectedImage5);
                    try {
                        images5 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage5);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    imageView5.setImageURI(selectedImage5);
                    String name5 = s5.substring(s5.lastIndexOf("/") + 1);
                    textview_imagename5.setText(name5);
                    imageclick_5 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                }
                break;
            case CAMERA_REQUEST_FIRST_IMAGE:
                if (resultCode == Activity.RESULT_OK) {
                    photo1 = (Bitmap) data.getExtras().get("data");
                    imageView1.setImageBitmap(photo1);
                    SaveImage(photo1);
                    textview_imagename1.setText(fname);
                    imageView2.setVisibility(View.VISIBLE);
                    imageclick_1 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                }
                break;
            case CAMERA_REQUEST_SECOND_IMAGE:
                if (resultCode == Activity.RESULT_OK) {
                    photo2 = (Bitmap) data.getExtras().get("data");
                    imageView2.setImageBitmap(photo2);
                    SaveImage(photo2);
                    textview_imagename2.setText(fname);
                    imageView3.setVisibility(View.VISIBLE);
                    imageclick_2 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                }
                break;
            case CAMERA_REQUEST_THIRD_IMAGE:
                if (resultCode == Activity.RESULT_OK) {
                    photo3 = (Bitmap) data.getExtras().get("data");
                    imageView3.setImageBitmap(photo3);
                    SaveImage(photo3);
                    textview_imagename3.setText(fname);
                    imageView4.setVisibility(View.VISIBLE);
                    imageclick_3 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                }
                break;
            case CAMERA_REQUEST_FOURTH_IMAGE:
                if (resultCode == Activity.RESULT_OK) {
                    photo4 = (Bitmap) data.getExtras().get("data");
                    imageView4.setImageBitmap(photo4);
                    SaveImage(photo4);
                    textview_imagename4.setText(fname);
                    imageView5.setVisibility(View.VISIBLE);
                    imageclick_4 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                }
                break;
            case CAMERA_REQUEST_FIFTH_IMAGE:
                if (resultCode == Activity.RESULT_OK) {
                    photo5 = (Bitmap) data.getExtras().get("data");
                    imageView5.setImageBitmap(photo5);
                    SaveImage(photo5);
                    textview_imagename5.setText(fname);
                    imageclick_5 = true;
                    deletebutton.setVisibility(View.VISIBLE);
                }
                break;
        }
    }

private void SaveImage(Bitmap finalBitmap) {
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/saved_images");
        myDir.mkdirs();
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        fname = "Image-" + timeStamp + ".jpg";
        File file = new File(myDir, fname);
        if (file.exists()) file.delete();
        try {
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageview1:
                if (imageclick_1) {
                    fullimageview.setVisibility(View.VISIBLE);
                    Bitmap bitmap = ((BitmapDrawable) imageView1.getDrawable()).getBitmap();
                    fullimageview.setImageBitmap(bitmap);
                } else {
                    final String[] Items = {"Click Pic", "Pick Pic", "Cancel"};
                    final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("Add Photo");
                    builder.setItems(Items, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            if (i == 0) {
                                isdialogset = "CAMERA";
                                if (imageView1.getDrawable() == null) {
                                    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                    startActivityForResult(takePicture, CAMERA_REQUEST_FIRST_IMAGE);
                                }
                            } else if (i == 1) {
                                isdialogset = "GALLERY";
                                Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//                                pickPhoto.setDataAndType(Uri.fromFile(new File(path)))
                                startActivityForResult(pickPhoto, PICK_PICTURE_FIRST_IMAGE);
                            } else {
                                dialogInterface.dismiss();
                            }
                        }
                    });
                    builder.show();
                }
                break;
            case R.id.imageview2:
                if (imageclick_2) {
                    fullimageview.setVisibility(View.VISIBLE);
                    Bitmap bitmap = ((BitmapDrawable) imageView2.getDrawable()).getBitmap();
                    fullimageview.setImageBitmap(bitmap);
                } else if (isdialogset.equals("CAMERA") && imageView2.getDrawable() == null) {
                    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(takePicture, CAMERA_REQUEST_SECOND_IMAGE);
                } else if (isdialogset.equals("GALLERY")) {
                    Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(pickPhoto, PICK_PICTURE_SECOND_IMAGE);
                }
                break;
            case R.id.imageview3:
                if (imageclick_3) {
                    fullimageview.setVisibility(View.VISIBLE);
                    Bitmap bitmap = ((BitmapDrawable) imageView3.getDrawable()).getBitmap();
                    fullimageview.setImageBitmap(bitmap);
                } else if (isdialogset.equals("CAMERA") && imageView3.getDrawable() == null) {
                    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(takePicture, CAMERA_REQUEST_THIRD_IMAGE);
                } else if (isdialogset.equals("GALLERY")) {
                    Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(pickPhoto, PICK_PICTURE_THIRD_IMAGE);
                }
                break;
            case R.id.imageview4:
                if (imageclick_4) {
                    fullimageview.setVisibility(View.VISIBLE);
                    Bitmap bitmap = ((BitmapDrawable) imageView4.getDrawable()).getBitmap();
                    fullimageview.setImageBitmap(bitmap);
                } else if (isdialogset.equals("CAMERA") && imageView4.getDrawable() == null) {
                    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(takePicture, CAMERA_REQUEST_FOURTH_IMAGE);
                } else if (isdialogset.equals("GALLERY")) {
                    Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(pickPhoto, PICK_PICTURE_FOURTH_IMAGE);
                }
                break;
            case R.id.imageview5:
                if (imageclick_5) {
                    fullimageview.setVisibility(View.VISIBLE);
                    Bitmap bitmap = ((BitmapDrawable) imageView5.getDrawable()).getBitmap();
                    fullimageview.setImageBitmap(bitmap);
                } else if (isdialogset.equals("CAMERA") && imageView5.getDrawable() == null) {
                    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(takePicture, CAMERA_REQUEST_FIFTH_IMAGE);
                } else if (isdialogset.equals("GALLERY")) {
                    Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(pickPhoto, PICK_PICTURE_FIFTH_IMAGE);
                }
                break;
        }
    }
  

代码已经过测试