4

Api data

{
  "student": [
    {
      "listing": {
        "price": 54,
      },
    },
 ]
}

I’m using below code for fetching data till listing:

let student = match val.ok()? {
  JsonValue::Object(obj) => {
     let (_, v) = obj.into_iter().find(|(k, _)| k.iter().copied().eq("student".chars()))?;
     match v {
        JsonValue::Array(vec) => vec,
        _ => return None,
     }
  },
  _ => return None,
};

for (i, obj) in instances.clone().into_iter().enumerate() {
  let list = match obj.clone() {
     JsonValue::Object(obj_data) => {
        let (_, v) = obj_data.into_iter().find(|(k, _)| k.iter().copied().eq("listings".chars()))?;
        match v {
           JsonValue::Object(list) => list,
           _ => return None,
        }
     },
     _ => return None,
  };

 // ?? price
}

Ques: How to fetch price from list?
Ques: I'm returning object from listing, Is that correct?

1 Answer 1

2

You can do this:

let (_, price_no) = list.into_iter().find(|(k, _)| k.iter().copied().eq("price".chars()))?;
            let p = match price_no {
                JsonValue::Number(list) => list,
                _ => return None,
            };
            let price = p.integer as u32;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.